
#include "include.h"



const u8 UartAscllArray[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};


Uart_TXFIFOType Uart_TXFIFO;

void Uart_Config(void)
{
	USART_InitTypeDef USART_InitStruct;
	UART_ClockCmd();
	USART_InitStruct.USART_BaudRate = UART_Baud;
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;
	USART_InitStruct.USART_StopBits = USART_StopBits_1;
	USART_InitStruct.USART_Parity = USART_Parity_No;
	USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART2,&USART_InitStruct);
	
	USART_Cmd(USART2,ENABLE);
}



void Uart_FIFOStoreFunction(u8*u8Data,u8 u8Length)
{
	u8 DataCnt=0;
	for(DataCnt=0;DataCnt<u8Length;DataCnt++)
	{
		Uart_TXFIFO.u8FIFOData[Uart_TXFIFO.u16FIFORearIndex++] = (u8)(*u8Data++);
		if(Uart_TXFIFO.u16FIFORearIndex >= UART_TXFIFOLength)
		{
			Uart_TXFIFO.u16FIFORearIndex = 0;
		}else{;}
	}
}

void Uart_16BitDataConvertFunction(u16 u16data)
{
	u8 u8DataArray[8] = {0};
	
	u8DataArray[0] = '0';
	u8DataArray[1] = 'x';
	u8DataArray[2] = UartAscllArray[((u16data>>12)&0xF)];
	u8DataArray[3] = UartAscllArray[((u16data>>8)&0xF)];
	u8DataArray[4] = UartAscllArray[((u16data>>4)&0xF)];
	u8DataArray[5] = UartAscllArray[((u16data>>0)&0xF)];
	u8DataArray[6] = '\r';
	u8DataArray[7] = '\n';
	
	Uart_FIFOStoreFunction(u8DataArray,8);
}

void Uart_32BitDataConvertFunction(u32 u32data)
{
	u8 u8DataArray[12] = {0};
	
	u8DataArray[0] = '0';
	u8DataArray[1] = 'x';
	u8DataArray[2] = UartAscllArray[((u32data>>28)&0xF)];
	u8DataArray[3] = UartAscllArray[((u32data>>24)&0xF)];
	u8DataArray[4] = UartAscllArray[((u32data>>20)&0xF)];
	u8DataArray[5] = UartAscllArray[((u32data>>16)&0xF)];
	u8DataArray[6] = UartAscllArray[((u32data>>12)&0xF)];
	u8DataArray[7] = UartAscllArray[((u32data>>8)&0xF)];
	u8DataArray[8] = UartAscllArray[((u32data>>4)&0xF)];
	u8DataArray[9] = UartAscllArray[((u32data>>0)&0xF)];
	u8DataArray[10] = '\r';
	u8DataArray[11] = '\n';
	
	Uart_FIFOStoreFunction(u8DataArray,12);
}

void Uart_TxMainFunction(void)
{
	if((Uart_TXFIFO.u16FIFORearIndex != Uart_TXFIFO.u16FIFOFrontIndex)&&(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == SET))
	{
		USART_SendData(USART2,Uart_TXFIFO.u8FIFOData[Uart_TXFIFO.u16FIFOFrontIndex++]);
		if(Uart_TXFIFO.u16FIFOFrontIndex>=UART_TXFIFOLength)
		{
			Uart_TXFIFO.u16FIFOFrontIndex=0;
		}else{;}
	}else{;}
}









